package cn.iocoder.yudao.module.security.controller.admin.risk;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.security.controller.admin.risk.vo.SecurityRiskPageReqVO;
import cn.iocoder.yudao.module.security.controller.admin.risk.vo.SecurityRiskRespVO;
import cn.iocoder.yudao.module.security.controller.admin.risk.vo.SecurityRiskSaveReqVO;
import cn.iocoder.yudao.module.security.dal.dataobject.risk.SecurityRiskDO;
import cn.iocoder.yudao.module.security.service.risk.SecurityRiskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 风险分析")
@RestController
@RequestMapping("/security/risk")
@Validated
public class SecurityRiskController {

    @Resource
    private SecurityRiskService riskService;

    @PostMapping("/create")
    @Operation(summary = "创建风险分析")
    @PreAuthorize("@ss.hasPermission('security:risk:create')")
    public CommonResult<Long> createRisk(@Valid @RequestBody SecurityRiskSaveReqVO createReqVO) {
        return success(riskService.createRisk(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新风险分析")
    @PreAuthorize("@ss.hasPermission('security:risk:update')")
    public CommonResult<Boolean> updateRisk(@Valid @RequestBody SecurityRiskSaveReqVO updateReqVO) {
        riskService.updateRisk(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除风险分析")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('security:risk:delete')")
    public CommonResult<Boolean> deleteRisk(@RequestParam("id") Long id) {
        riskService.deleteRisk(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得风险分析")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('security:risk:query')")
    public CommonResult<SecurityRiskRespVO> getRisk(@RequestParam("id") Long id) {
        SecurityRiskDO risk = riskService.getRisk(id);
        return success(BeanUtils.toBean(risk, SecurityRiskRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得风险分析分页")
    @PreAuthorize("@ss.hasPermission('security:risk:query')")
    public CommonResult<PageResult<SecurityRiskRespVO>> getRiskPage(@Valid SecurityRiskPageReqVO pageReqVO) {
        PageResult<SecurityRiskDO> pageResult = riskService.getRiskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SecurityRiskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出风险分析 Excel")
    @PreAuthorize("@ss.hasPermission('security:risk:export')")
    @OperateLog(type = EXPORT)
    public void exportRiskExcel(@Valid SecurityRiskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SecurityRiskDO> list = riskService.getRiskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "风险分析.xls", "数据", SecurityRiskRespVO.class,
                        BeanUtils.toBean(list, SecurityRiskRespVO.class));
    }

}